﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhysicalDataProvider;
using DTOFrameworks;
using System.Data.Common;
using FooFun.DataElements.Dtos;

namespace PasswordForm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DEDataProvider dataProvider = new DEDataProvider();

            AccountService accountService = new AccountService();
            accountService.DataProvider = dataProvider;
            PermissionService pService = new PermissionService();
            pService.DataProvider = dataProvider;

            dataProvider.Begin();

            IDataElement aDe = accountService.ListDataElement();
            IDataElement pDe = pService.ListDataElement();

            dataProvider.End();

            foreach (IDataElement de in aDe)
            {
                IdName account = new IdName();
                account.Id = (string)de["AccountId"].Value;
                account.Name
                    = (string)de["AccountName"].Value;

                int index = comboBox1.Items.Add(account);
            }

            foreach (IDataElement de in pDe)
            {
                IdName account = new IdName();
                account.Id = (string)de["PermissionId"].Value;
                account.Name
                    = (string)de["Permission"].Value;

                int index = comboBox2.Items.Add(account);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IdName account = comboBox1.SelectedItem as IdName;
            IdName per = comboBox2.SelectedItem as IdName;

            SqlHelper.ExecuteNonQuery(
                string.Format("INSERT INTO t_PermissionSets ([AccountId],[PermissionId],[ForumId],[PermissionSet]) VALUES ('{0}','{1}','{2}',{3})",
                account.Id, per.Id, textBox1.Text, checkBox1.Checked),
                new DbParameter[] { });

            MessageBox.Show("插入成功！");
        }
    }

    public class IdName
    {
        string accountId;

        public string Id
        {
            get { return accountId; }
            set { accountId = value; }
        }

        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public override string ToString()
        {
            return this.name;
        }
    }
}
